﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using LabMS.BLL;
using System.Collections.Generic;

namespace LabMS.Web.LabPersonnel
{
    public partial class ListPositionLog : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("GWRZGL");
            strPageUserType.Add("mana");
            strPageUserType.Add("teacher");
            
            //checkall.Attributes.Add("onclick", "checkAllChanged(this, 'gvPositionLogList')");
            PLogKeyWordTime1.Attributes.Add("onclick", "d.showMoreDay = false;d.show(this)");
            PLogKeyWordTime2.Attributes.Add("onclick", "d.showMoreDay = false;d.show(this)");
            if (!IsPostBack)
            {
                BindGridViewList("");
            }
            PLogKeyWordTime1.Attributes.Add("readOnly","-1");
            PLogKeyWordTime2.Attributes.Add("readOnly", "-1");
        }

        //为GridView绑定数据
        private void BindGridViewList(string strWhere)
        {
            LabMS.BLL.PositionLog positionLog = new LabMS.BLL.PositionLog();

            string loginUserType = UserType;
            string loginUserCode = UserCode;
            if (loginUserType.Equals("mana"))
            {
                strWhere += " ";
            }
            if (loginUserType.Equals("teacher"))
            {
                strWhere += " PLog_TeacherCode='" + loginUserCode + "'";
            }
            List<LabMS.Model.PositionLog> positionLogList = positionLog.GetModelList(strWhere);

            gvPositionLogList.DataSource = positionLogList;
            gvPositionLogList.DataBind();
        }

        //删除人员岗位日志
        protected void Delete_OnClick(object sender, EventArgs e)
        {
            CheckBox chb;
            LabMS.BLL.PositionLog pLog = new LabMS.BLL.PositionLog();
            try
            {
                for (int i = 0; i < gvPositionLogList.Rows.Count; i++)
                {
                    chb = (CheckBox)gvPositionLogList.Rows[i].FindControl("PositionLogCB");
                    if (chb.Checked)
                    {
                        string PLID = gvPositionLogList.DataKeys[i].Value.ToString();
                        pLog.Delete(int.Parse(PLID));
                    }
                }
                BindGridViewList(SearchString());
                LabMS.Common.JShelper.JSAlert(Page, "deleteSuccess", "删除成功!");
            }
            catch
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "删除失败!");
            }
        }

        //搜索查询语句
        protected string SearchString()
        {
            string strWhere = " 1=1 ";
            if (!string.IsNullOrEmpty(PLogKeyWordTime1.Text.Trim()))
            {              
                strWhere += " and PLog_Time >= '" + PLogKeyWordTime1.Text.Trim() + "'";                
            }
            if (!string.IsNullOrEmpty(PLogKeyWordTime2.Text.Trim()))
            {
                strWhere += " and PLog_Time <= '" + PLogKeyWordTime2.Text.Trim() + "'";
            }

            return strWhere;
        }
        //按日期搜索
        protected void btSearch_Click(object sender, EventArgs e)
        {
            BindGridViewList(SearchString());
        }

        //分页
        protected void GridViewPLog_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvPositionLogList.PageIndex = e.NewPageIndex;
            BindGridViewList(SearchString());
        }

        protected void GridViewPLog_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //判断是否为数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //鼠标移动时改变背景色
                //e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='White';this.style.color='#003399'");
                //e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='6699FF';this.style.color='8C4510'");
                e.Row.Attributes["style"] = "Cursor:pointer";
                //鼠标点击数据行时的链接
                int itemp = e.Row.Cells.Count;
                for (int i = 1; i < itemp; i++)
                {
                    e.Row.Cells[i].Attributes.Add("onDblclick", "window.location='PositionLogPreview.aspx?ID="
                       + gvPositionLogList.DataKeys[gvPositionLogList.DataKeys.Count-1].Value.ToString() + "'");
                }
                
            }
        }
    }
}
